package com.flower.service.impl;

import com.flower.dao.FlowerMapper;
import com.flower.dao.ImgsMapper;
import com.flower.pojo.Flower;
import com.flower.pojo.Imgs;
import com.flower.service.FlowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FlowerServiceImpl implements FlowerService {


    @Autowired
    private FlowerMapper flowerMapper;
    @Autowired
    private ImgsMapper imgsMapper;

    @Override
    public Integer queryAllFlower() {
        return flowerMapper.queryAllFlower();
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
    public int addFlower(Flower flower, List<Imgs> list) throws Exception {
        if (flowerMapper.addFlower(flower) != -1) {
            for (Imgs imgs : list) {
                imgs.setFid(flower.getFid());
            }
            if (imgsMapper.addImgs(list) != -1) {
                return 1;
            } else {
                throw new Exception();
            }
        } else {
            throw new Exception();
        }
    }

    @Override
    public List<Flower> queryStoreFlower(Integer sid) {
        return flowerMapper.queryStoreFlower(sid);
    }
}
